package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.TeachQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Teach;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ColumnInfoDaoImpl类
 * @date 2017-03-16
 */

public class TeachDaoImpl extends CustomBaseSqlDaoImpl implements TeachDaoCustom {

    public PageModel<Teach> queryTeachPage(TeachQueryDTO teachQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Teach t where 1=1 and t.deleteFlag = 0 ");
        if (teachQueryDTO != null) {
            if (StringUtils.isNotBlank(teachQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + teachQueryDTO.getName() + "%");
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, teachQueryDTO.getCurrentPage(), teachQueryDTO.getPageSize());
    }

    public List<Teach> queryTeachList(TeachQueryDTO teachQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from ColumnInfo t where 1=1 and t.deleteFlag = 0 ");
        if (teachQueryDTO != null) {
            if (StringUtils.isNotBlank(teachQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + teachQueryDTO.getName() + "%");
            }
        }
        return this.queryByMapParams(hql.toString(), map);
    }
    @Override
    public int updateSelectiveById(Teach teach) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update Teach t set");
        if (teach != null) {

            if (!StringUtils.isNotBlank(teach.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(teach.getName())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.name = :name ");
                }else {
                    hql.append(" t.name = :name ");
                }
                map.put("name", teach.getName());
            }

            if (StringUtils.isNotBlank(teach.getHeadImg())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.headImg = :headImg ");
                }else {
                    hql.append(" t.headImg = :headImg ");
                }
                map.put("headImg", teach.getHeadImg());
            }
            if (StringUtils.isNotBlank(teach.getIndustry())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.industry = :industry ");
                }else {
                    hql.append(" t.industry = :industry ");
                }
                map.put("industry", teach.getIndustry());
            }


            if (StringUtils.isNotBlank(teach.getDeleteFlag())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.deleteFlag = :deleteFlag ");
                } else {
                    hql.append(" t.deleteFlag = :deleteFlag ");
                }
                map.put("deleteFlag", teach.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(teach.getDescription())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.description = :description ");
                } else {
                    hql.append(" t.description = :description ");
                }
                map.put("description", teach.getDescription());
            }
            hql.append("where t.id=:id ");
            map.put("id", teach.getId());
        }
        return this.update(hql.toString(), map);
    }

    public PageModel<Map<String, Object>> queryTeachList(PageBean pageBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,DATE_FORMAT(create_time,'%Y-%m-%d') as createTime ,name,description, industry,head_img as headImg FROM teach where delete_flag=0 order by create_time desc ");
        String baseSql = "SELECT count(*) FROM teach where delete_flag=0 ";
        return this.querySqlObjects(hql.toString(), baseSql, map, pageBean.getCurrentPage()+1, pageBean.getPageSize());
    }

}